package com.mingtu.jgspatrol.fragment;

import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.center.activity.RecordActivity;
import com.mingtu.center.bean.StatisticsBean;
import com.mingtu.common.base.BaseBindingFragment;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.MyLogUtil;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.databinding.Tab3Binding;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

public class Tab3Page extends BaseBindingFragment<Tab3Binding> implements LazyFragmentPagerAdapter.Laziable {
    
    @Override
    protected int getLayoutId() {
        return R.layout.tab3;
    }

    @Override
    protected void initData() {
        addClickListener(this, binding.ivHistoryData);
    }

    @Override
    protected void initView() {
        initListener();
    }

    @Override
    protected void getData() {
        getStatisticsData();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_history_data:
                IntentUtils.getInstance().readyGo(getActivity(), RecordActivity.class);
                break;
        }
    }
    
    private void initListener() {
        initPullRefresh();//刷新
    }

    private void initPullRefresh() {
        binding.srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                binding.srlUp.finishRefresh(Constant.REFRESH_TIME);
                binding.srlUp.setNoMoreData(false);
                binding.srlUp.setEnableLoadMore(false);
                getStatisticsData();
            }
        });
    }


    /**
     * 获取统计数据
     */
    private void getStatisticsData() {
        OkGo.<String>post(Constant.getInstance().POST_STAT)
                .tag(this)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            StatisticsBean bean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                bean = new StatisticsBean();
                                bean = gson.fromJson(result, StatisticsBean.class);

                                StatisticsBean.CurrentWeekBean currentWeekBean = bean.getCurrentWeek();

                                if (currentWeekBean != null) {


                                    String beat = currentWeekBean.getBeat();//百分数
                                    String grade = currentWeekBean.getGrade();//巡护分数
                                    String taskMileage = currentWeekBean.getTaskMileage();// 巡护里程
                                    String taskDuration = currentWeekBean.getTaskDuration();// 巡护时长
                                    String taskNum = currentWeekBean.getTaskNum();// 巡护次数
                                    String eventNum = currentWeekBean.getEventNum();// 上报次数
                                    String picNum = currentWeekBean.getEventPic();// 上图片张数
                                    String voicNum = currentWeekBean.getEventVioce();// 音频个数
                                    String videoNum = currentWeekBean.getEventVideo();// 视频个数
                                    String ocrNum = currentWeekBean.getOcrNum();// 识别个数

                                    if (!StringUtils.isEmpty(beat)) {
                                        binding.tvBeat.setText(beat);
                                    }
                                    if (!StringUtils.isEmpty(grade)) {
                                        binding.tvGrade.setText(grade);
                                    }

                                    if (!StringUtils.isEmpty(taskMileage)) {
                                        binding.tvTraskMileage.setText(taskMileage + "km");
                                    }

                                    if (!StringUtils.isEmpty(taskDuration)) {

                                        long d = 0;
                                        if (taskDuration.indexOf(".") != -1) {
                                            String array[] = taskDuration.split("\\.");
                                            d = Long.parseLong(array[0]);
                                        } else {

                                            d = Long.parseLong(taskDuration);
                                        }
                                        binding.tvTraskDuration.setText(MyUtills.formatSeconds(d));
                                    }
                                    if (!StringUtils.isEmpty(taskNum)) {
                                        binding.tvTraskTimes.setText(taskNum + "次");
                                    }

                                    if (!StringUtils.isEmpty(eventNum)) {
                                        binding.tvEventNum.setText(eventNum + "次");
                                    }
                                    if (!StringUtils.isEmpty(picNum)) {
                                        binding.tvEventPic.setText(picNum + "张");
                                    }
                                    if (!StringUtils.isEmpty(voicNum)) {
                                        binding.tvEventVioce.setText(voicNum + "个");
                                    }
                                    if (!StringUtils.isEmpty(videoNum)) {
                                        binding.tvEventVideo.setText(videoNum + "个");
                                    }
                                    if (!StringUtils.isEmpty(ocrNum)) {
                                        binding.tvOcrNum.setText(ocrNum);
                                    }
                                }
                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        String result = response.body();
                        MyLogUtil.e(getClass().getSimpleName(), "onError==" + result);

                    }
                });
    }
}